<?php
namespace app\back\model;

use think\Model;

class Account extends Model
{
    protected $table = 'account';

	protected $readonly = ['mobile'];
	protected $type = [
        'state'    =>  'integer',
        'gender'    =>  'integer',
        'create_time'     =>  'datetime',
        'update_time'  =>  'datetime',
        'delete_time'      =>  'datetime',
    ];

    /*
     * 定义关联属性
     */
    public function moneyRelation() {
        $return_fields = 'amount_money,recharge_money,give_money,last_consumption_time,diy_times,fish_times,rollball_times';
        return $this->hasOne('AccountMoney', 'mobile', 'mobile')->field($return_fields);
    }

    /*
     * 定义关联属性
     */
    public function ticketRelation() {
        return $this->hasMany('AccountTickets', 'mobile', 'mobile');
    }

    /*
     * 定义关联属性
     */
    public function recordRelation() {
        return $this->hasMany('AccountRecord', 'mobile', 'mobile');
    }

	/*
	 * 获取器
	 * 
	 */
    public function getStateAttr($value)
    {
        $status = $this->stateConfigArr();

        return $status[$value];
    }

    public function stateConfigArr() {
        return [-1=>'删除',1=>'正常',2=>'禁用'];
    }

    public function getGenderAttr($value)
    {
        $status = $this->genderConfigArr();

        return $status[$value];
    }

    public function genderConfigArr() {
        return [0=>'女士',1=>'先生',2=>'公公'];
    }

    public function getGenderText() {
        $gender = $this->getData('gender');
        if($gender === 2) {
            return '';
        }

        return $this->genderConfigArr()[$gender];
    }

    public function getLevelAttr($value)
    {
        $status = $this->genderLevelArr();

        return $status[$value];
    }

    public function genderLevelArr() {
        return [0=>'普通会员',1=>'充值会员',2=>'办卡会员'];
    }

    static function existsByMobile($mobile) {
        return Account::where(['mobile'=>$mobile])->count('id');
    }

    /*
     * 根据手机获取用户id
     */
    static function getPidByMobile($mobile) {
        return Account::where(['mobile'=>$mobile])->value('id');
    }

    /*
     * 根据手机获取用户id
     */
    static function getMobileByOpenid($openid) {
        return Account::where(['wx_openid'=>$openid])->where('state', '1')->value('mobile');
    }

    /*
     * 根据手机获取用户id
     */
    static function getInfoByOpenid($openid) {
        return Account::where(['wx_openid'=>$openid])->where('state', '1')->find();
    }

    public function getMoneyDetail() {
    }

    /*
     * 查询微信会员存在
     */
    public function WXAcountExists($openid) {
        return $this->where('wx_openid', $openid)->count();
    }

    /*
     * 注册微信用户
     */
    public function registerWXAccount($mobile, $openid) {
        $wxuser_model = new \app\common\model\WXUser;

        $userinfo = $wxuser_model->where('username', $openid)->find();

        if($this->where('mobile', $mobile)->count()) {
            $update_data = [
                'nickname' => $userinfo['nickname'],
                'wx_openid' => $openid,
                'pic' => $userinfo['headimgurl'],
            ];
            return $this->where('mobile', $mobile)->update($update_data);
        } else {
            $this->mobile = $mobile;
            $this->nickname = $userinfo['nickname'];
            $this->wx_openid = $openid;
            $this->pic = $userinfo['headimgurl'];
            return $this->save();
        }
    }
}
